package com.vodafone.nowplus.android.peoplemail.connector;

import com.vodafone.nowplus.android.peoplemail.connector.I360PeopleConnector.PeopleStatus;

import android.util.Log;

/**
 * @author mschlech
 * 
 *         PeopleConnector class A Connector which will obtain the
 *         MailCredentials from the local app specific database. The credentials
 *         will be the username and password. Those credentials can be used to
 *         get a valid authentication at the IMAP server.
 */

public class PeopleConnector implements I360PeopleConnector {

	private final I360PeopleConnector.MailCredentials credentials = new I360PeopleConnector.MailCredentials();

	private String username = "";

	private String password = "";

	private String sessionID = "";

	private boolean appDB = false;

	private int stateBits = 00;

	public PeopleConnector() {

		this.getPeopleStatus();
		// @TODO implement the status in a dedicated method
		// this.peopleStatus = getPeopleStatus();
	}

	/**
	 * method to have the following states to be determined Username & password
	 * exist , session ID does exist => LOGGED_IN(11) Username & password exist
	 * , session ID does not exist => NOT_LOGGED_IN(01) Username & password is
	 * empty no sessionID => NOT_REGISTERED(00) Database does not exist =>
	 * NOT_INSTALLED (10)
	 */
	private int getPeopleStatus() {

		/**
		 * ContentResolver cr = new ContentResolver() appDB = cr.getDBState();
		 * sessionID = cr.getSessionID(); usernname = cr.getUserName(); password
		 * = cr.getPassword();
		 */

		/**
		 * fields for controll structure to get state assumed 00 = initial value
		 * 
		 */

		if (username.length() == 0 & password.length() == 0 & sessionID.length() == 0) {
			stateBits = 00;
		} else if (username.length() != 0 & password.length() != 0 & sessionID.length() != 0) {
			stateBits = 11;
		} else if (username.length() != 0 & password.length() != 0 & sessionID.length() == 0) {
			stateBits = 01;
		} else if (appDB == false) {
			stateBits = 10;
		}
		return stateBits;

	}

	/**
	 * @param stateBits
	 */
	public void providePeopleStatus(int stateBits) {

		credentials.username = "<noUser>";
		credentials.password = "<noPassword>";
		switch (stateBits) {
		case 00:
			// not Logged in
			credentials.status = I360PeopleConnector.PeopleStatus.NOT_REGISTERED;
			break;
		case 11:
			credentials.status = I360PeopleConnector.PeopleStatus.LOGGED_IN;
			break;
		case 10:
			credentials.status = I360PeopleConnector.PeopleStatus.NOT_INSTALLED;
			break;
		case 01:
			credentials.status = I360PeopleConnector.PeopleStatus.NOT_LOGGED_IN;
			break;
		default:
			break;
		}
	}



	@Override
	public MailCredentials getMailCredentials() {
		providePeopleStatus(this.getPeopleStatus());
		Log.i("Status of user => ", " status[" + credentials.status + "]" + " Username [" + credentials.username
				+ "] password [ " + credentials.password + "]");
		return credentials;
	}

}
